package com.yuyou.fn.tas.pusher.handler;

import com.alibaba.fastjson.JSONArray;
import com.google.common.collect.Collections2;
import com.yuyou.fn.common.database.event.EventHandler;
import com.yuyou.fn.common.database.event.IEventHandler;
import com.yuyou.fn.common.util.BeanUtils;
import com.yuyou.fn.educational.entity.CourseClass;
import com.yuyou.fn.tas.pusher.service.IDataPushService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;


@EventHandler(tables = "yyfn.course_class")
@Component
public class CourseClassPusher implements IEventHandler {

    @Resource
    private IDataPushService dataPushService;


    @Override
    public void insert(String tableName, JSONArray rowList) {
        handleCourseCou(rowList);
    }

    @Override
    public void update(String tableName, JSONArray rowList, Set<String> updateColumns) {
        handleCourseCou(rowList);
    }

    @Override
    public void delete(String tableName, JSONArray rowList) {

    }

    private void handleCourseCou(JSONArray rowList) {

        List<CourseClass> courseClasses = rowList.toJavaList(CourseClass.class);


        Map<Long,List<CourseClass>> areaTeamId2CourseClass = BeanUtils.toGroup(new ArrayList<>(courseClasses),"areaTeamId");

        for(Map.Entry<Long,List<CourseClass>> entry : areaTeamId2CourseClass.entrySet() ){
            dataPushService.pushCourseClass(entry.getKey(),entry.getValue());
        }

    }
}
